/* Copyright (c) 2010, Peter Lényi */
/* Licensed under The BSD License. */

package gem.apps.fs.db;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Manager class FileManagerImpl provides an implementation of the FileManager interface.
 * 
 * @author Peter Lényi
 * @version 20100421F
 */
public class FileManagerImpl implements FileManager
{
    private static FileManager instance = null;

    private static final String UPLOAD_DIR = "GFS_UL_Dir" + System.getProperty("file.separator");
    static {
        File ulDir = new File(UPLOAD_DIR);
        if (!ulDir.exists()) {
            ulDir.mkdirs();
        }
        ulDir.setReadable(true, false);
        ulDir.setWritable(true, false);
        ulDir.setExecutable(false, false);
    }
    private static final String EXT = ".fsf";

    private FileManagerImpl() { /* SINGLETON, FACTORY METHOD PATTERNS */ }

    /**
     * @return class instance.
     */
    public static FileManager getInstance() {
        if (instance == null) {
            instance = new FileManagerImpl();
        }
        return instance;
    }

    public void storeFile(File file, InputStream is) throws FileStorageException {
        try {
            OutputStream os = new FileOutputStream(UPLOAD_DIR + file.getName() + EXT);
            int b = -1;
            while ((b = is.read()) > -1) {
                os.write(b);
            }
        }
        catch (Exception ex) {
            throw new FileStorageException("File could not be stored.", ex);
        }
    }

    public boolean exists(long uin) throws FileStorageException {
        boolean result = false;
        try {
            File file = new File(UPLOAD_DIR + uin + EXT);
            result = file.exists();
        }
        catch (Exception ex) {
            throw new FileStorageException("Existance of the file could not been verified.", ex);
        }
        return result;
    }

    public boolean isFile(long uin) throws FileStorageException {
        boolean result = false;
        try {
            File file = new File(UPLOAD_DIR + uin + EXT);
            result = file.isFile();
        }
        catch (Exception ex) {
            throw new FileStorageException("Nature of the file could not been verified..", ex);
        }
        return result;
    }

    public FileInputStream loadFile(long uin) throws FileStorageException {
        FileInputStream result = null;
        try {
            result = new FileInputStream(UPLOAD_DIR + uin + EXT);
        }
        catch (Exception ex) {
            throw new FileStorageException("File could not be retrieved.", ex);
        }
        return result;
    }

    public void deleteFile(long uin) throws FileStorageException {
        try {
            File file = new File(UPLOAD_DIR + uin + EXT);
            file.delete();
        }
        catch (Exception ex) {
            throw new FileStorageException("File could not be deleted.", ex);
        }
    }
}
